﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ShakeIt_.ServiceReference2;

namespace ShakeIt_
{
    public partial class researchresultPage : PhoneApplicationPage
    {
        private List<Grid> itemList = new List<Grid>();
        private List<TextBlock> titleBoxList = new List<TextBlock>();
        private List<TextBlock> createrBoxList = new List<TextBlock>();
        private List<TextBlock> timeBoxList = new List<TextBlock>();
        private List<TextBlock> addressBoxList = new List<TextBlock>();
        

        public researchresultPage()
        {
            InitializeComponent();
            addAllInList();
            SearchContent.Text = Data.searchInput;
            
            displayActivity(Data.result);
            Data.proxy.FindActivityByLocationCompleted += new EventHandler<FindActivityByLocationCompletedEventArgs>(FindActivityCompleted);
            Data.proxy.FindActivityByLocationAndKeywordCompleted += new EventHandler<FindActivityByLocationAndKeywordCompletedEventArgs>(FindActivityKeywordCompleted);
        }

        private void researchresultPage_loaded(object sender, RoutedEventArgs e)
        {
            SearchContent.Text = Data.searchInput;
        }

        private void SearchContent_TextChanged(object sender, RoutedEventArgs e)
        {
            Data.searchInput = SearchContent.Text;
            Data.FindActivity();
        }

        private void FindActivityCompleted(object sender, FindActivityByLocationCompletedEventArgs e)
        {
            displayActivity(Data.result);
        }

        private void FindActivityKeywordCompleted(object sender, FindActivityByLocationAndKeywordCompletedEventArgs e)
        {
            displayActivity(Data.result);
        }

        private void displayActivity(List<_Activity> res)
        {
            Message.Text = "总共搜索到" + res.Count + "个活动";
            Data.res_Count = 0;
            for (int i = 0; i < 10; i++)
            {
                itemList[i].Visibility = Visibility.Collapsed;
            }
            foreach (_Activity acti in res)
            {
                itemList[Data.res_Count].Visibility = Visibility.Visible;
                titleBoxList[Data.res_Count].Text = acti.title;
                addressBoxList[Data.res_Count].Text = acti.address;
                timeBoxList[Data.res_Count].Text = acti.beginTime.ToString() + "-" + acti.endTime.ToString();
                createrBoxList[Data.res_Count].Text = acti.createUser.username;
                Data.res_Count++;
            }
        }

        private static int ActivityDistanceCompration(_Activity obj1, _Activity obj2)
        {
            int res = 0;
            if ((obj1 == null) && (obj2 == null))
            {
                return 0;
            }
            else if ((obj1 != null) && (obj2 == null))
            {
                return 1;
            }
            else if ((obj1 == null) && (obj2 != null))
            {
                return -1;
            }
            if (obj1.distance > obj2.distance)
            {
                res = 1;
            }
            else if (obj1.distance < obj2.distance)
            {
                res = -1;
            }
            return res;
        }

        private static int ActivityBeginTimeCompration(_Activity obj1, _Activity obj2)
        {
            int res = 0;
            if ((obj1 == null) && (obj2 == null))
            {
                return 0;
            }
            else if ((obj1 != null) && (obj2 == null))
            {
                return 1;
            }
            else if ((obj1 == null) && (obj2 != null))
            {
                return -1;
            }
            if (obj1.beginTime > obj2.beginTime)
            {
                res = 1;
            }
            else if (obj1.beginTime < obj2.beginTime)
            {
                res = -1;
            }
            return res;
        }

        private void typeeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Data.resList_selected = new List<_Activity>();
            int type_index = typeeBox.SelectedIndex;
            List<string> typelist = new List<string>();
            typelist.Add("按类型");
            typelist.Add(typee1.Text);
            typelist.Add(typee2.Text);
            typelist.Add(typee3.Text);
            typelist.Add(typee4.Text);
            string selectedType = typelist[type_index];
            if (Data.result != null)
            {
                if (selectedType == "按类型" | selectedType == "全部")
                {
                    Data.resList_selected = Data.result;
                    displayActivity(Data.result);
                }
                else
                {
                    foreach (_Activity acti in Data.result)
                    {
                        if (acti.type == selectedType)
                        {
                            Data.resList_selected.Add(acti);
                        }
                    }
                    displayActivity(Data.resList_selected);
                }
            }
        }

        private void switch_to_map_Click(object sender, RoutedEventArgs e)
        {
            if (this.NavigationService.CanGoBack &&
                this.NavigationService.BackStack.ToArray()[0].Source.OriginalString == "/nearsearchPage.xaml")
            {
                this.NavigationService.GoBack();
            }
            else
                this.NavigationService.Navigate(new Uri("/nearsearchPage.xaml", UriKind.Relative));
        }

        private void distance_button_Click(object sender, RoutedEventArgs e)
        {
            List<_Activity> res = Data.result;
            res.Sort(ActivityDistanceCompration);
            displayActivity(res);
        }

        private void beginTime_button_Click(object sender, RoutedEventArgs e)
        {
            List<_Activity> res = Data.result;
            res.Sort(ActivityBeginTimeCompration);
            displayActivity(res);
        }

        private void resultList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int resultList_index = resultList.SelectedIndex;
            Data.res_selected = Data.resList_selected[resultList_index];
            this.NavigationService.Navigate(new Uri("/detailPage.xaml", UriKind.Relative));
        }

        private void addAllInList()
        {
            itemList.Add(item0);
            itemList.Add(item1);
            itemList.Add(item2);
            itemList.Add(item3);
            itemList.Add(item4);
            itemList.Add(item5);
            itemList.Add(item6);
            itemList.Add(item7);
            itemList.Add(item8);
            itemList.Add(item9);
            titleBoxList.Add(titleBox0);
            titleBoxList.Add(titleBox1);
            titleBoxList.Add(titleBox2);
            titleBoxList.Add(titleBox3);
            titleBoxList.Add(titleBox4);
            titleBoxList.Add(titleBox5);
            titleBoxList.Add(titleBox6);
            titleBoxList.Add(titleBox7);
            titleBoxList.Add(titleBox8);
            titleBoxList.Add(titleBox9);
            titleBoxList.Add(titleBox0);
            titleBoxList.Add(titleBox0);
            titleBoxList.Add(titleBox0);
            createrBoxList.Add(createrBox0);
            createrBoxList.Add(createrBox1);
            createrBoxList.Add(createrBox2);
            createrBoxList.Add(createrBox3);
            createrBoxList.Add(createrBox4);
            createrBoxList.Add(createrBox5);
            createrBoxList.Add(createrBox6);
            createrBoxList.Add(createrBox7);
            createrBoxList.Add(createrBox8);
            createrBoxList.Add(createrBox9);
            timeBoxList.Add(timeBox0);
            timeBoxList.Add(timeBox1);
            timeBoxList.Add(timeBox2);
            timeBoxList.Add(timeBox3);
            timeBoxList.Add(timeBox4);
            timeBoxList.Add(timeBox5);
            timeBoxList.Add(timeBox6);
            timeBoxList.Add(timeBox7);
            timeBoxList.Add(timeBox8);
            timeBoxList.Add(timeBox9);
            addressBoxList.Add(addressBox0);
            addressBoxList.Add(addressBox1);
            addressBoxList.Add(addressBox2);
            addressBoxList.Add(addressBox3);
            addressBoxList.Add(addressBox4);
            addressBoxList.Add(addressBox5);
            addressBoxList.Add(addressBox6);
            addressBoxList.Add(addressBox7);
            addressBoxList.Add(addressBox8);
            addressBoxList.Add(addressBox9);

        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }
    }

}